package com.hss01248.dialog.bottomsheet;

import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.Rect;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.util.Optional;

/**
 *
 * 沉浸式状态栏参考自: http://www.jianshu.com/p/08755838c00f
 *
 * 解决滑动关闭后不能打开的问题: http://blog.csdn.net/yanzhenjie1003/article/details/51938425?locationNum=1&fps=1
 * Created by Administrator on 2017/12/10.
 */

public class RightMdBottomSheetDialog extends CommonDialog {
    private Context ctx = null;
    public RightMdBottomSheetDialog(Context context) {
        super(context);
        ctx = context;
    }

    public RightMdBottomSheetDialog( Context context, int theme) {
        super(context);
        ctx = context;
    }

    @Override
    protected void onCreate() {
        getWindow().setLayoutAlignment(LayoutAlignment.BOTTOM);
        getWindow().clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        getWindow().clearWindowFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_LAYOUT_INSET_DECOR);
        getWindow().addWindowFlags(WindowManager.LayoutConfig.MARK_LAYOUT_INSET_DECOR);
        super.onCreate();
    }
    @Override
    protected void onShow()
    {
        int dialogHeight = getScreenHeight(ctx);
        Rect rect = getWindow().getBoundRect();
        if (rect != null) {
            if (rect.bottom == rect.top) {
                rect.top = rect.bottom - dialogHeight;
            }
            getWindow().setBoundRect(rect);
        }
        super.onShow();
    }

    public static int getScreenHeight(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        return display.get().getAttributes().height;
    }
}
